package com.zt.rongda.quota.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zt.rongda.quota.inter.QuotaConfigMapper;
import com.zt.rongda.quota.model.QuotaConfig;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class QuotaConfigService extends BaseService {

	@Autowired
	private QuotaConfigMapper quotaConfigMapper;

	/**
	 * 查询
	 *
	 * @param reqBean
	 * @return
	 */
	public JSONArray getQuotaConfigList(ReqBean reqBean) {
		SqlBuilder sql_parent = new SqlBuilder(
				"select qc.QUOTA_CONFIG_ID ,qc.QUOTA_CONFIG_NAME from QUOTA_CONFIG qc where qc.QUOTA_CONFIG_PARENT_ID=0 ");
		JSONArray parentQuotaConfigArray = this.query(sql_parent);
		for (int i = 0; i < parentQuotaConfigArray.size(); i++) {
			JSONObject parentQuotaConfig = parentQuotaConfigArray.getJSONObject(i);
			String parentId = parentQuotaConfig.getString("quotaConfigId");
			SqlBuilder sql_son = new SqlBuilder(
					"select qc.QUOTA_CONFIG_ID ,qc.QUOTA_CONFIG_NAME,qc.QUOTA from QUOTA_CONFIG qc where qc.QUOTA_CONFIG_PARENT_ID= ? ",
					parentId);
			JSONArray sonQuotaConfigArray = this.query(sql_son);
			parentQuotaConfig.put("sonQuotaConfigArray", sonQuotaConfigArray);
		}
		return parentQuotaConfigArray;
	}

	/**
	 * 保存定额配置
	 */
	public void saveQuotaConfig(ReqBean reqBean) {
		Date date = new Date();
		JSONArray array = JSONArray.fromObject(reqBean.getObjectInfo("quotaConfigList"));
		for (int i = 0; i < array.size(); i++) {
			JSONObject quotaJson = array.getJSONObject(i);
			JSONArray sonQuotaConfigArray = quotaJson.getJSONArray("sonQuotaConfigArray");
			for (int j = 0; j < sonQuotaConfigArray.size(); j++) {
				JSONObject sonQuotaConfig = sonQuotaConfigArray.getJSONObject(j);
				String quotaConfigId = sonQuotaConfig.getString("quotaConfigId");
				double quota = sonQuotaConfig.getDouble("quota");
				QuotaConfig quotaConfig = getById(quotaConfigId);
				quotaConfig.setQuota(quota);
				quotaConfig.setMakeTime(date);
				quotaConfig.setModifyTime(date);
				quotaConfigMapper.updateByPrimaryKeySelective(quotaConfig);
			}
		}
	}

	/**
	 * 通过id查询
	 *
	 * @param publishTypeId
	 * @param makeTypeId
	 * @return
	 */
	public QuotaConfig getById(String quotaConfigId) {
		return quotaConfigMapper.selectByPrimaryKey(quotaConfigId);
	}
	
	/**
	 * 通过发布类型、制作类型查询
	 *
	 * @param publishTypeId
	 * @param makeTypeId
	 * @return
	 */
	public QuotaConfig getByPublishTypeAndMakeType(String publishTypeId,String makeTypeId) {
		return quotaConfigMapper.selectByPublishTypeAndMakeType(publishTypeId,makeTypeId);
	}
	/**
	 * 通过定额配置父类型查询
	 *
	 * @param publishTypeId
	 * @param makeTypeId
	 * @return
	 */
	public QuotaConfig getByQuotaConfigParentId(String qotaConfigParentId) {
		return quotaConfigMapper.selectByQuotaConfigParentId(qotaConfigParentId);
	}
	
	/**
	 * 通过发布类型查询父类型id
	 *
	 * @param publishTypeId
	 * @param makeTypeId
	 * @return
	 */
	public String getByPublishType(String publishTypeId) {
		List<QuotaConfig>  quotaConfigList = quotaConfigMapper.selectByPublishType(publishTypeId);
		QuotaConfig  quotaConfig = new QuotaConfig();
		if(quotaConfigList!=null && quotaConfigList.size()>0){
			quotaConfig = quotaConfigList.get(0);
		}
		return quotaConfig.getQuotaConfigParentId();
	}

	/**
	 * 新增
	 *
	 * @param quotaConfig
	 */
	public void add(QuotaConfig quotaConfig) {
		quotaConfig.setQuotaConfigId(stringId());

		quotaConfig.setMakeTime(new Date());
		quotaConfig.setModifyTime(quotaConfig.getMakeTime());
		quotaConfigMapper.insert(quotaConfig);
	}

	/**
	 * 修改
	 *
	 * @param quotaConfig
	 */
	public void update(QuotaConfig quotaConfig) {
		String publishTypeId = quotaConfig.getPublishTypeId();
		String makeTypeId = quotaConfig.getMakeTypeId();
		if (StringUtil.isEmpty(publishTypeId) || StringUtil.isEmpty(makeTypeId)) {
			throw new BusinessException("获取主键失败");
		}

		quotaConfig.setModifyTime(new Date());
		quotaConfigMapper.updateByPrimaryKeySelective(quotaConfig);
	}

	/**
	 * 删除
	 *
	 * @param publishTypeId
	 * @param makeTypeId
	 */
	public void delete(String quotaConfigId) {
		if (StringUtil.isEmpty(quotaConfigId)) {
			throw new BusinessException("获取主键失败");
		}
		quotaConfigMapper.deleteByPrimaryKey(quotaConfigId);
	}

}